<?php


class Model_Status
{
    /** Model_Table_Status */
    protected $_table;


    /*mode */
    const HEAT=0;
    const COOL=1;    

    /* vent */
    const OPEN=0;
    const CLOSE=1;

    /**
     * Retrieve table object
     * 
     * @return Model_Status_Table
     */
    public function getTable()
    {
        if (null === $this->_table) {
            // since the dbTable is not a library item but an application item,
            // we must require it to use it
            require_once APPLICATION_PATH . '/models/DbTable/Status.php';
            $this->_table = new Model_DbTable_Status;
        }
        return $this->_table;
    }

    /**
     * Save a new status
     * 
     * @param  array $data 
     * @return int|string
     */
    public function save(array $data)
    {
        $table  = $this->getTable();
        $fields = $table->info(Zend_Db_Table_Abstract::COLS);
        foreach ($data as $field => $value) {
            if (!in_array($field, $fields)) {
                unset($data[$field]);
            }
        }
        return $table->insert($data);
    }

    /**
     * Fetch all entries
     * 
     * @return Zend_Db_Table_Rowset_Abstract
     */
    public function fetchEntries()
    {
        // we are gonna return just an array of the data since
        // we are abstracting the datasource from the application,
        // at current, only our model will be aware of how to manipulate
        // the data source (dbTable).
        // This ALSO means that if you pass this model
        return $this->getTable()->fetchAll('1')->toArray();    
    }

    /**
     * Fetch an individual entry
     * 
     * @param  int|string $id 
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntry($id)
    {
        $table = $this->getTable();
        $select = $table->select()->where('status_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $rs =  $table->fetchRow($select)->toArray();
        if (count($rs) > 0) { 
          return $rs->toArray();
        } else {
          return array();
        }
    }


    /**
     * Fetch status by zone_id
     * 
     * @param  int|string $zone_id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchbyZoneId($zone_id)
    {
        $table = $this->getTable();
        $select = $table->select()->where('zone_id = ?', $zone_id);

        // see reasoning in fetchEntries() as to why we return only an array
        $rs =  $table->fetchAll($select);
        if (count($rs) > 0) { 
          return $rs->toArray();
        } else {
          return array();
        }
    }
        
    /** 
     * Fetch last entry 
     *
     * @param  int|string $zone_id 
     * @return Zend_Db_Table_Rowset_Abstract
     */
     public function fetchLatest($zone_id)
     {
         $table = $this->getTable();
         $select = $table->select()->where('zone_id = ?', $zone_id)
                                   ->order('status_id desc')->limit(1);
         // see reasoning in fetchEntries() as to why we return only an array
         //return $table->fetchRow($select)->toArray();
         $rowset = $table->fetchRow($select);
         $rowCount = count($rowset);
         if ($rowCount > 0) {
           $rs =  $rowset->toArray();
           $rs["vent_state"] = ($rs["vent_state"] == Model_Status::CLOSE) ? "Close" : "Open";
           $rs["mode"]       = ($rs["mode"] == Model_Status::COOL) ? "Cooling" : "Heating";
           return $rs;
         } else {
           return array();
         }
     }
}